

import requests
from lxml import etree
import os

if __name__ == '__main__':
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
                      ' Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.76'
    }
    url = 'https://www.aqistudy.cn/historydata/'
    page_text = requests.get(url = url,headers = headers).text
    tree = etree.HTML(page_text)
    hot_city_list = tree.xpath('//div[@class = "hot"]//ul/li/a/text()')
    # print(hot_city_list)
    all_city_name = tree.xpath('//div[@class = "all"]//ul/div[2]//a/text()')
    # print(all_city_name)
    i =int(0)
    for city in all_city_name:
        i+=1
        print(city,i)
    all_city_href = tree.xpath('//div[@class = "all"]//ul/div[2]//a/@href')
    # for city_href in all_city_href:
    #     for city in all_city_name:
    #         print(city+"网址是：https://www.aqistudy.cn/historydata/"+city_href+'/n')

    # allall_city = []
    # all_city_list = tree.xpath('//div[@class = "all"]//ul/div[2]')
    # for city_name in all_city_list:
    #     all_city_name = city_name.xpath('.//a/text()')
    #     all_city_href = city_name.xpath('.//a/@href')
    #     allall_city.append(all_city_name)
    #     # print(all_city_href)
    #     print(all_city_name)
    # # for i in range(1,200):
    # # all_city_href = 'https://www.aqistudy.cn/historydata/'+str(tree.xpath('//div[@class = "all"]//ul/div[2]//a/@href'))
    # # print(all_city_href)
    #     fp = open('./city.txt','w',encoding='utf-8')
    #     fp.write(str(allall_city))

